//
//  NSString+YUCheck.m
//
//  Created by BruceYu on 15/9/7.
//  Copyright (c) 2015年 BruceYu. All rights reserved.
//

#import "NSString+check.h"

@implementation NSString (check)
-(BOOL)checkPhoneLength{
    if ([self length]>=11 && [self length]<=14) {
        return YES;
    }
    return NO;
}



-(BOOL)isPhoneNo
{
    
    if (!kObjectIsEmpty(self))return NO;
    
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,181,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|7[0678]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,181,189
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:self] == YES)
        || ([regextestcm evaluateWithObject:self] == YES)
        || ([regextestct evaluateWithObject:self] == YES)
        || ([regextestcu evaluateWithObject:self] == YES))
    {
        if([regextestcm evaluateWithObject:self] == YES) {
            NSLog(@"China Mobile");
        } else if([regextestct evaluateWithObject:self] == YES) {
            NSLog(@"China Telecom");
        } else if ([regextestcu evaluateWithObject:self] == YES) {
            NSLog(@"China Unicom");
        } else {
            NSLog(@"Unknow");
        }
        
        return YES;
    }
    else
    {
        return NO;
    }
}
-(BOOL)isCallNo
{
    BOOL ret = FALSE;
    NSString *regex =  @"^((\\+86)|(\\(\\+86\\)))?\\D?((((010|021|022|023|024|025|026|027|028|029|852)|(\\(010\\)|\\(021\\)|\\(022\\)|\\(023\\)|\\(024\\)|\\(025\\)|\\(026\\)|\\(027\\)|\\(028\\)|\\(029\\)|\\(852\\)))\\D?\\d{8}|((0[3-9][1-9]{2})|(\\(0[3-9][1-9]{2}\\)))\\D?\\d{7,8}))(\\D?\\d{1,4})?$";
    
    if (IsSafeString(self)) {
        NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        ret = [pred evaluateWithObject:self];
    }
    return  ret;
}

-(BOOL)isPassWord
{
    //6-20位数字和字母组成
    
    NSString *regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
    
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if ([pred evaluateWithObject:self]) {
        
        return YES ;
        
    }else
        
        return NO;
    
}

-(BOOL)isPhoneNumber
{
    //正则表达式
    
    NSString *pattern = @"^1+[3578]+\\d{9}$";
    
    //创建一个谓词,一个匹配条件
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    
    //评估是否匹配正则表达式
    
    BOOL isMatch = [pred evaluateWithObject:self];
    
    return isMatch;
    
}

-(BOOL)isLocateCallNo
{
    BOOL ret = FALSE;
    NSString *regex =  @"^(([0-9]{7})|(([0-9]{8})))$";
    
    
    if (IsSafeString(self)) {
        NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        ret = [pred evaluateWithObject:self];
    }
    return  ret;
}

-(BOOL)is400PhoneNo
{
    BOOL ret = FALSE;
    NSString *regex =  @"^400[016789]\\d{6}$";
    
    if (IsSafeString(self)) {
        NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        ret = [pred evaluateWithObject:self];
    }
    return  ret;
}


-(BOOL)isValidEmail
{
    NSString *emailPattern =
    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:emailPattern options:NSRegularExpressionCaseInsensitive error:&error];
    NSTextCheckingResult *match = [regex firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    return match != nil;

}

-(BOOL)isPwd
{
    return  IsSafeString(self) && self.length >= 6 && self.length <= 16 ;
}
-(BOOL)isNumberic
{
    BOOL ret = FALSE;
    
    NSString *regex = @"^[0-9]*$";
    if (IsSafeString(self)) {
        NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        ret = [pred evaluateWithObject:self];
    }
    return ret;
}

//-(BOOL)isDigit
//{
//    BOOL ret = FALSE;
//    NSString *regex =  @"^-?\\d+$";
//    
//    if (IsSafeString(self)) {
//        NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//        ret = [pred evaluateWithObject:self];
//    }
//    return  ret;
//}
-(BOOL)isDecimal //小数
{
    
    BOOL ret = FALSE;
    
    NSString *regex = @"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";//@"^\\d{1}$|^\\d{1}$.\\d{1}$";//@"^\\d{1,}$|^\\d{1,}.\\d{2,2}$"; //@"^(0|[1-9]/d*)/.(/d+)$";
    if (IsSafeString(self)) {
        NSPredicate * pred  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        ret = [pred evaluateWithObject:self];
    }
    return ret;
}



- (BOOL)isWhitespaceAndNewlines {
    NSCharacterSet* whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    for (NSInteger i = 0; i < self.length; ++i) {
        unichar c = [self characterAtIndex:i];
        if (![whitespace characterIsMember:c]) {
            return NO;
        }
    }
    return YES;
}

- (BOOL)isEmptyOrWhitespace {
    return !self.length ||
    ![self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length;
}


- (BOOL)isURLString{
    NSString *emailRegEx =@"^http(s)?://([\\w-]+.)+[\\w-]+(/[\\w-./?%&=]*)?$";
    
    NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
    return [regExPredicate evaluateWithObject:[self lowercaseString]];
}

- (BOOL)isHasString:(NSString*)substring{
    return !([self rangeOfString:substring].location == NSNotFound);
}

//有非法字符
-(BOOL)isIncludeSpecialCharact{
    //***需要过滤的特殊字符：~￥#&*<>《》()[]{}【】^@/￡¤￥|§¨「」『』￠￢￣~@#￥&*（）——+|《》$_€。
    NSRange urgentRange = [self rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @"~￥#&*<>《》()[]{}【】^@/￡¤￥|§¨「」『』￠￢￣~@#￥&*（）——+|《》$_€"]];
    if (urgentRange.location == NSNotFound)
    {
        return NO;
    }
    return YES;
}
//验证身份证号是否有效
-(BOOL)validateIdentityCard
{
    /*
     - (BOOL)validateIdentityCard {
     BOOL flag;
     if (self.length <= 0) {
     flag = NO;
     return flag;
     }
     NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
     NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
     return [identityCardPredicate evaluateWithObject:self];
     }

    */
    
    
        NSString *value = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSInteger length = value.length;
        NSString *str = [value substringFromIndex:15];
        if([str isEqualToString:@"000"])
        {
            length = 15;
        }
        
        
        
        // 省份代码
        
        NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41",@"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
        
        
        
        NSString *valueStart2 = [value substringToIndex:2];
        
        BOOL areaFlag =NO;
        
        for (NSString *areaCode in areasArray) {
            
            if ([areaCode isEqualToString:valueStart2]) {
                
                areaFlag =YES;
                
                break;
                
            }
            
        }
        
        
        
        if (!areaFlag) {
            
            return NO;
            
        }
        
        
        
        
        
        NSRegularExpression *regularExpression;
        
        NSUInteger numberofMatch;
        
        
        
        int year =0;
        
        switch (length) {
                
            case 15:
                
                year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
                
                
                
                if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                    
                    
                    
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:nil];//测试出生日期的合法性
                    
                    
                }else {
                    
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:nil];//测试出生日期的合法性
                    
                    
                }
                
                numberofMatch = [regularExpression numberOfMatchesInString:value
                                 
                                                                   options:NSMatchingReportProgress
                                 
                                                                     range:NSMakeRange(0, value.length - 3)];
                
                
                
                
                if(numberofMatch >0) {
                    
                    return YES;
                    
                }else {
                    
                    return NO;
                    
                }
                
            case 18:
                
                
                
                year = [value substringWithRange:NSMakeRange(6,4)].intValue;
                
                if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                    
                    
                    
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                         
                                                                            options:NSRegularExpressionCaseInsensitive
                                         
                                                                              error:nil];//测试出生日期的合法性
                    
                }else {
                    
                    regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                         
                                                                            options:NSRegularExpressionCaseInsensitive
                                         
                                                                              error:nil];//测试出生日期的合法性
                    
                }
                
                numberofMatch = [regularExpression numberOfMatchesInString:value
                                 
                                                                   options:NSMatchingReportProgress
                                 
                                                                     range:NSMakeRange(0, value.length)];
                
                
                
                if(numberofMatch >0) {
                    
                    int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
                    
                    int Y = S %11;
                    
                    NSString *M =@"F";
                    
                    NSString *JYM =@"10X98765432";
                    
                    M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                    
                    if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
                        
                        return YES;// 检测ID的校验位
                        
                    }else {
                        
                        return NO;
                        
                    }
                    
                    
                    
                }else {
                    
                    return NO;
                    
                }
                
            default:
                
                return NO;
                
        }
    }

-(BOOL)isEmptyString{
    if (self.length <= 0 || [self isEqualToString:@""] || !self) {
        return YES;
    }
    return NO;
}

-(NSString *)isEmpty
{
    if ([self isEmptyString]) {
        return @"";
    } else if ([self isKindOfClass:[NSNull class]])
    {
        return @"";
    }
    return self;
}

@end
